package com.n9.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class StringUtils {
//	private static final String BASESTRING = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

//	private static final String FOLDER_SEPARATOR = "/";
//
//	private static final String WINDOWS_FOLDER_SEPARATOR = "\\";
//
//	private static final String TOP_PATH = "..";
//
//	private static final String CURRENT_PATH = ".";
//
//	private static final char EXTENSION_SEPARATOR = '.';


	//---------------------------------------------------------------------
	// General convenience methods for working with Strings
	//---------------------------------------------------------------------

	/**
	 * Check if a String has length.
	 * <p><pre>
	 * StringUtils.hasLength(null) = false
	 * StringUtils.hasLength("") = false
	 * StringUtils.hasLength(" ") = true
	 * StringUtils.hasLength("Hello") = true
	 * </pre>
	 * @param str the String to check, may be <code>null</code>
	 * @return <code>true</code> if the String is not null and has length
	 */
	public static boolean hasLength(String str) {
		return (str != null && str.length() > 0);
	}

	/**
	 * Check if a String has text. More specifically, returns <code>true</code>
	 * if the string not <code>null<code>, it's <code>length is > 0</code>, and
	 * it has at least one non-whitespace character.
	 * <p><pre>
	 * StringUtils.hasText(null) = false
	 * StringUtils.hasText("") = false
	 * StringUtils.hasText(" ") = false
	 * StringUtils.hasText("12345") = true
	 * StringUtils.hasText(" 12345 ") = true
	 * </pre>
	 * @param str the String to check, may be <code>null</code>
	 * @return <code>true</code> if the String is not null, length > 0,
	 * and not whitespace only
	 * @see Character#isWhitespace
	 */
	public static boolean hasText(String str) {
		int strLen;
		if (str == null || (strLen = str.length()) == 0) {
			return false;
		}
		for (int i = 0; i < strLen; i++) {
			if (!Character.isWhitespace(str.charAt(i))) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 截取字符串 不包含边界
	 * @param src 原数据
	 * @param beginStr 开始字符串
	 * @param endStr   结束字符串
	 * @return
	 */
	public static String subString(String src, String beginStr, String endStr) {
		if (!"".equals(beginStr)) {
			int beginIndex = src.indexOf(beginStr);
			if (beginIndex == -1) {
				return "";
			}
			beginIndex += beginStr.length();
			int endIndex = src.indexOf(endStr, beginIndex);
			if (endIndex == -1) {
				return "";
			}
			return src.substring(beginIndex, endIndex);
		} else {
			return src.substring(0, src.indexOf(endStr));
		}
	}
	/**
	 * 截取字符串
	 * @param src 原数据
	 * @param beginStr 开始字符串
	 * @return
	 */
	public static String subString(String src, String beginStr) {
//		Assert.hasLength(src);
//		Assert.hasLength(beginStr);
		final int indexOf = src.indexOf(beginStr);
		if (indexOf == -1) {
			return null;
		}
		int beginIndex = indexOf + beginStr.length();
		
		return src.substring(beginIndex);
	}
	
	/**
	 * 把字符串按regex规则拆分为int数组
	 * @param str	原字符串
	 * @param regex 拆分符
	 * @return numbers int数组
	 */
	public static int[] splitStringToIntArray(String str, String regex) {
//		Assert.hasText(str);
//		Assert.hasLength(regex);
		
		String[] strs = str.split(regex);
		int[] numbers = new int[strs.length];
		for (int i = 0; i < strs.length; i++) {
			numbers[i] = Integer.parseInt(strs[i]);
		}
		
		return numbers;
	}
	
	/**
	 * 在字符串srcStr中是否包含目标字符串targetStr
	 * @param srcStr 原字符串 
	 * @param target 目标字符串
	 * @return
	 */
	public static boolean contain(String srcStr, String targetStr) {
//		Assert.hasLength(srcStr);
//		Assert.hasLength(targetStr);
		
		return srcStr.indexOf(targetStr) >= 0;
	}
	/**
	 * 截取字符串,不包含开始字符串和结束字符串，匹配不成功时返回空字符串
	 * @param sourceStr 源字符串
	 * @param beginStr 第一次匹配index
	 * @param endStr 最后一次匹配index
	 * @param bigScope true: endIndex取最后一个匹配，flase：endIndex取beginIndex之后的第一个匹配
	 * @return 新字符串
	 */
	public static String subString(String sourceStr, String beginStr, String endStr, boolean bigScope){
		String ret = "";
		int begin = sourceStr.indexOf(beginStr);
		if (begin == -1) {
			return ret;
		}
		int end = -1;
		if (bigScope) {
			end = sourceStr.lastIndexOf(endStr);
		} else {
			end = sourceStr.indexOf(endStr, begin);
		}
		if (end == -1) {
			return ret;
		}
		begin += beginStr.length();
		if (begin >= end) {
			return ret;
		}
		ret = sourceStr.substring(begin, end);
		return ret;
	}
	
	/**
	 * @param sourceStr 源字符串
	 * @param beginStr 开始字符串
	 * @param endStr 结束字符串
	 * @param hasSides 是否包含边界
	 * @return
	 */
	public static List<String> subStrings(String sourceStr, String beginStr, String endStr, boolean hasSides) {
		List<String> ret = new ArrayList<String>();
		int beginLength = beginStr.length();
		int endLength = endStr.length();
		int fromIndex = 0;
		while (true) {
			int begin = sourceStr.indexOf(beginStr, fromIndex);
			if (begin == -1) {
				return ret;
			}
			fromIndex = begin + beginLength;
			if (!hasSides) {
				begin = fromIndex;
			}
			int end = sourceStr.indexOf(endStr, fromIndex);
			if (end == -1) {
				return ret;
			}
			
			fromIndex = end + endLength;
			if (hasSides) {
				end = fromIndex;
			}
			ret.add(sourceStr.substring(begin, end));
		}
	}

	/**
	 * @param sourceStr 源字符串
	 * @param regex 正则表达式
	 * @return 匹配的字符串集合List&lt;String&gt;
	 */
	public static List<String> subStrings(String sourceStr, String regex) {
		List<String> ret = new ArrayList<String>(0);
		Pattern pt = Pattern.compile(regex);
		Matcher matcher = pt.matcher(sourceStr);
		while (matcher.find()) {
			ret.add(matcher.group());
		}
		return ret;
	}
	/**
	 * 截取字符串，包含边界
	 * @param sourceStr 源字符串
	 * @param beginStr 开始字符串
	 * @param endStr 结束字符串
	 * @return 
	 */
	public static List<String> subStringsWithSides(String sourceStr, String beginStr, String endStr) {
		return subStrings(sourceStr, beginStr, endStr, true);
	}
	/**
	 * 截取字符串，不包含边界
	 * @param sourceStr 源字符串
	 * @param beginStr 开始字符串
	 * @param endStr 结束字符串
	 * @return 
	 */
	public static List<String> subStrings(String sourceStr, String beginStr, String endStr) {
		return subStrings(sourceStr, beginStr, endStr, false);
	}
	
	/**
	 * 
	 * 判断字符是否为数字
	 * 
	 * @param str		
	 * @return
	 */
	public static boolean isNumber(String str) {
		return Pattern.matches("\\d+", str);
	}
}
